package com.frx.fakeroutexposed;

import android.annotation.TargetApi;
import android.content.Context;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;

import com.frx.fakeroutexposed.tools.MY_NAMES;
import com.frx.fakeroutexposed.tools.SettingsHelper;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;

/**
 * @author loki
 * @version :2.0
 *          2017年2月28日 20:36:16
 *          利用 xposed 框架hook 森林巡检系统
 *          com.senruansoft.forestrygis 森林巡检系统3.0.0
 */
public class GPShook implements IXposedHookLoadPackage {
    /**
     * 只读的SharedPreferences
     */
    private SettingsHelper mSettings = new SettingsHelper();

    @Override
    public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
        // 获取目标程序的包名
        String targetPackageName =
                mSettings.getString(MY_NAMES.KEY_TARGET_PACKAGE_NAME
                        , MY_NAMES.DATA_defalut_target_package_name);
        if (!(lpparam.packageName
                .equals(targetPackageName))) {
            //如果不是目标程序,则退出 Hook
            return;
        }
        XposedBridge.log("SR-Loaded app: " + lpparam.packageName);


        /**hook 高德的函数 setMockEnable(boolean paramBoolean)
         * 允许目标程序接收模拟位置
         *
         * ===============================
         * xposed的作者对于匿名类说:
         * Once the member has been initialized (e.g. after the constructor was run),
         * you could get the member's value to find out its class. Otherwise,
         * you would have to know the name that the compiler assigned to the anonymous class (e.g. MemberClass$1).
         * ===============================
         * */
        findAndHookMethod(
                "com.amap.api.location.AMapLocationClientOption",
                lpparam.classLoader, "setMockEnable", boolean.class, new XC_MethodHook() {

                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param)
                            throws Throwable {
                        //方法调用之前 hook 将参数修改为true,允许目标程序模拟位置
                        Object[] args = param.args;
                        args[0] = true;
                        param.args = args;
                    }//end beforeHookedMethod

                });//end findAndHookMethod

        //==================================
        //TODO 在设置界面加入该选项
        /**hook 目标程序的 com.senruansoft.forestrygis.util.l 类中的 <br>
         * public static String getImei(Context paramContext) 方法<br>
         * 破解目标程序与机器的绑定
         * */
        findAndHookMethod(
                "com.senruansoft.forestrygis.util.l",
                lpparam.classLoader, "getImei", Context.class, new XC_MethodHook() {

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        //修改返回的 IMEI 号为自定义的 IMEI
                        String mIMEIString=mSettings.getString(MY_NAMES.KEY_IMEI,"");
                        if(
                                ("".equals(mIMEIString))
                                        ||(null==mIMEIString)){//如果 IMEI 为空,则 return
                            return;
                        }
                        XposedBridge.log("SR-fakeIMEI: " + mIMEIString);
                        param.setResult(mIMEIString);
                    }//end afterHookedMethod
                });//end findAndHookMethod


    }// end handleLoadPackage

}//end Class GPShook.java
